import { _decorator, Component, Label, tween, v3, Tween, Vec3, UITransform, UIOpacity } from 'cc';
import { PoolManager } from '../Managers/PoolManager';
import { UIManager } from '../Managers/UIManager';

const { ccclass, property } = _decorator;

@ccclass('Tip')
export default class Tip extends Component {
    maskUT: UITransform = null!;
    tipUT: UITransform = null!;
    tipLb: Label = null!;
    uiOpacity: UIOpacity = null!;

    onLoad() {
        this.maskUT = this.node.getChildByName(`Mask`).getComponent(UITransform);
        this.tipUT = this.node.getChildByName(`TipLb`).getComponent(UITransform);;
        this.tipLb = this.node.getChildByName(`TipLb`).getComponent(Label);
        this.uiOpacity = this.node.getComponent(UIOpacity);
    }

    Show(content: string, delay: number = 0.5, type: number = 1) {
        this.uiOpacity.opacity = 255;
        this.node.setPosition(Vec3.ZERO);
        this.node.setScale(Vec3.ONE);

        this.tipLb.string = `${content}`;
        this.tipLb.updateRenderData(true);
        this.maskUT.setContentSize(this.tipUT.contentSize.width + 100, this.tipUT.contentSize.height + 10);

        Tween.stopAllByTarget(this.node);
        Tween.stopAllByTarget(this.uiOpacity);

        switch (type) {
            case 1:
                this.ShowTween1(delay);
                break;
            case 2:
                this.ShowTween2(delay);
                break;
            default:
                this.ShowTween1(delay);
                break;
        }
    }

    ShowTween1(delay: number) {
        this.node.setScale(Vec3.ZERO);

        tween(this.node)
            .to(0.5, { scale: v3(1, 1) }, { easing: `elasticOut` })
            .delay(delay)
            .to(0.25, { position: v3(0, 150), scale: v3(0, 0) })
            .call(() => {
                PoolManager.PutNode(this.node);
            })
            .start();
    }

    ShowTween2(delay: number) {
        tween(this.uiOpacity).delay(delay).to(0.25, { opacity: 0 }).start();
        tween(this.node).delay(delay).to(0.25, { position: v3(0, 250) }).call(() => { PoolManager.PutNode(this.node); }).start();
    }



}
